package com.ruoyi.lipin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.lipin.domain.LpStrategy;
import com.ruoyi.lipin.domain.bo.LpStrategyBo;
import com.ruoyi.lipin.domain.vo.LpStrategyVo;
import com.ruoyi.lipin.mapper.LpStrategyMapper;
import com.ruoyi.lipin.service.ILpStrategyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 攻略Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-08
 */
@RequiredArgsConstructor
@Service
public class LpStrategyServiceImpl implements ILpStrategyService {

    private final LpStrategyMapper baseMapper;

    @Override
    public TableDataInfo<LpStrategy> selectPageStrategyList(LpStrategy strategy, PageQuery pageQuery) {
        LambdaQueryWrapper<LpStrategy> lqw = new LambdaQueryWrapper<LpStrategy>()
            .like(StringUtils.isNotBlank(strategy.getTitle()), LpStrategy::getTitle, strategy.getTitle())
            .like(StringUtils.isNotBlank(strategy.getCreateBy()), LpStrategy::getCreateBy, strategy.getCreateBy());
        lqw.orderByDesc(LpStrategy::getCreateTime);
        Page<LpStrategy> page = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    /**
     * 查询攻略
     */
    @Override
    public LpStrategyVo queryById(Long strategyId){
        return baseMapper.selectVoById(strategyId);
    }

    /**
     * 查询攻略列表
     */
    @Override
    public TableDataInfo<LpStrategyVo> queryPageList(LpStrategyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LpStrategy> lqw = buildQueryWrapper(bo);
        Page<LpStrategyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询攻略列表
     */
    @Override
    public List<LpStrategyVo> queryList(LpStrategyBo bo) {
        LambdaQueryWrapper<LpStrategy> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LpStrategy> buildQueryWrapper(LpStrategyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LpStrategy> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), LpStrategy::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), LpStrategy::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getCoverImage()), LpStrategy::getCoverImage, bo.getCoverImage());
        return lqw;
    }

    /**
     * 新增攻略
     */
    @Override
    public Boolean insertByBo(LpStrategyBo bo) {
        LpStrategy add = BeanUtil.toBean(bo, LpStrategy.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setStrategyId(add.getStrategyId());
        }
        return flag;
    }

    /**
     * 修改攻略
     */
    @Override
    public Boolean updateByBo(LpStrategyBo bo) {
        LpStrategy update = BeanUtil.toBean(bo, LpStrategy.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LpStrategy entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除攻略
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
